﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TCKQKL.FrontEnd.DataModel;
using TCKQKL.FrontEnd.Config;
using TCKQKL.Service.LookupClient;
using System.Web.Security;
using System.Globalization;

namespace TCKQKL.FrontEnd.Web.Controls
{
    public partial class BankTransactions : System.Web.UI.UserControl
    {
        public List<TransDataUI> trans = new List<TransDataUI>();
        public List<InvestorDataUI> assigns;
        public BankDataUI bankData = new BankDataUI();
        public decimal remainStart = 0;
        public decimal remainEnd = 0;
        public decimal lockedAmount = 0;
        public decimal nettingAmount = 0;
        public decimal totalIncAmount = 0;
        public decimal totalDecAmount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region CheckAuthentication
                string userid = HttpContext.Current.User.Identity.Name;
                if (userid == "")
                {
                    HttpCookie authcookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                    if (authcookie == null)
                    {
                        FormsAuthentication.SignOut();
                        Response.Redirect(Settings.URL_PAGE_LOGIN);
                    }
                    FormsAuthenticationTicket ticket = (FormsAuthenticationTicket)FormsAuthentication.Decrypt(authcookie.Value);
                    userid = ticket.Name;
                }
                #endregion
                #region Parameters
                DateTime fromDate = DateTime.Now.AddDays(-2);
                DateTime toDate = DateTime.Now;
                #endregion
                try
                {                    
                    if (IsPostBack)
                    {
                        fromDate = Convert.ToDateTime(date1.Text, new CultureInfo("en-US")).AddDays(-1);
                        toDate = Convert.ToDateTime(date2.Text, new CultureInfo("en-US"));
                    }
                    //dateRange.Text = "Từ ngày " + fromDate.ToString("dd-MM-yyyy") + " đến ngày " + toDate.ToString("dd-MM-yyyy");
                    LSClient client = new LSClient();
                    decimal investorID = client.getAccountData(userid).investorId;
                    remainStart = client.getRemain(investorID, fromDate);
                    trans = client.getTransByInvestorID(investorID, fromDate.Date.AddDays(1), toDate);
                    bankData = client.getBankDataByDate(investorID, toDate);
                    nettingAmount = client.getNettingAmountByInvestorId(investorID);
                    client.Close();
                }
                catch
                {
                    Label lblAlert = (Label)Page.Master.FindControl("lblFullName");
                    lblAlert.Text = Settings.ERROR_CONNECT_SERVICE;
                    return;
                }
            }
            catch
            {
                throw;
            }
        }
    }
}